﻿using DevLib.SuperTool;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


/** 
 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
 * Description: Show the log about which method failed.
 * Author: Heller (HellerSong@outlook.com)
 * CreationTime: 3/17/2015 8:32:24 AM
 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** 
 **/
namespace DevLib.Logger
{
    public class DevLogger
    {
        private const long MAX_SIZE = 3 * 1024 * 1024;   // 3M, log file max size
        public static String logFile = Path.Combine(DevGlobal.PATH_PROJECT, "AnLogFolder", "LogDetail.log");


        public static void WriteLog(String log)
        {
            StreamWriter logWriter = new StreamWriter(logFile, true);

            if (new FileInfo(logFile).Length >= MAX_SIZE)
                CleanLog();

            /*
			 * Get who call writelog()
			 */
            StackFrame[] stackFrames = new StackTrace(true).GetFrames();
            String callingMethodName = stackFrames[1].GetMethod().Name.ToString();
            // Jump method which name starts with does, such as "doesPathExists" in HSCheck
            int i = 2;
			while (DevRegex.CheckIsMatch(callingMethodName, DevRegex.CheckStartWithDoes))
            {
                callingMethodName = stackFrames[i].GetMethod().Name.ToString();
                i++;
            }
            // Method name is: Invoke at index i -1
            callingMethodName = stackFrames[i].GetMethod().Name.ToString();

            /*
			 * Get formated time string
			 */
            String timeString = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + " > ";
            log = timeString + "MethodName: " + callingMethodName + " | " + "Conten: " + log;

            logWriter.WriteLine(log);
            logWriter.Close();
        }

        public static void WriteLogContent(String logContent)
        {
            StreamWriter logWriter = new StreamWriter(logFile, true);
            string timeString = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + " > ";
            logWriter.WriteLine("".PadRight(timeString.Length) + "\t{ " + logContent + " }");
            logWriter.Close();
        }

		/// <summary> Override "LogDetail.txt.old" if exists otherwise copy from region. </summary>
        private static void CleanLog()
        {
            String oldLogFile = logFile + ".old";
            StreamWriter sw;

            if (File.Exists(oldLogFile))
            {
                sw = new StreamWriter(oldLogFile, true);
                sw.WriteLine(File.ReadAllText(logFile));
                sw.Close();
            }
            else
                File.Copy(logFile, oldLogFile);
            sw = new StreamWriter(logFile, true);
            sw.Write("");
            sw.Close();
        }

		/// <summary> Get who call the WriteLog(), add Calling Method Name to region msg. </summary>
		/// <param name="msg">Region message</param>
		/// <returns></returns>
        public static String GetCallingExceptionMessage(String msg)
        {
            StackFrame[] stackFrames = new StackTrace(true).GetFrames();
            String callingMethodName = stackFrames[1].GetMethod().Name.ToString();
            // Jump method which name starts with does, such as "doesPathExists" in HSCheck
            int i = 2;
			while (DevRegex.CheckIsMatch(callingMethodName, DevRegex.CheckStartWithDoes))
            {
                callingMethodName = stackFrames[i].GetMethod().Name.ToString();
                i++;
            }
            // Method name is: Invoke at index i -1
            callingMethodName = stackFrames[i].GetMethod().Name.ToString();
            msg = "My Exception > MethodName: " + callingMethodName + " | " + "Conten: " + msg;

            return msg;
        }



    }
}
